package com.ee.chat.api.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.ee.chat.api.interceptor.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
@EnableAsync
@EnableScheduling
public class WebConfig implements WebMvcConfigurer {
    @Resource
    LoginInterceptor loginInterceptor;
    @Resource
    LogInterceptor logInterceptor;
    @Resource
    UserStatusInterceptor userStatusInterceptor;
    @Resource
    CheckSumInterceptor checkSumInterceptor;
    @Resource
    CorsInterceptor corsInterceptor;

    /**
     * 开始mybatis-plus分页
     * @Author Shane
     * @Date 2019/8/2616:38
     * @param
     * @return
     **/
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作， true调回到首页，false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addPathPatterns("/**") 表示拦截所有的请求，
        // excludePathPatterns("/login", "/register") 表示除了登陆与注册之外，因为登陆注册不需要登陆也可以访问
        //解决跨域
        registry.addInterceptor(corsInterceptor).addPathPatterns("/**");

        //打印请求日志
        registry.addInterceptor(logInterceptor).addPathPatterns("/**");

        //接口校验
        registry.addInterceptor(checkSumInterceptor).addPathPatterns("/**").excludePathPatterns("/error",
                "/test/*", "/payNotify/*",
                "/im/imCallback", "/im/imCopyCallback", "/base/getNewestVersion", "/base/ossCallback", "/base/addClickLog");

        //拦截未登录
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns("/error", "/login/login",
                "/base/*", "/test/*", "/payNotify/*",
                "/im/imCallback", "/im/imCopyCallback", "/base/getNewestVersion", "/base/ossCallback", "/base/addClickLog",
                "/host/getBanners", "/host/getTitleHosts", "/host/getTypeHosts", "/host/getRandomOnlineHost", "/host/getUserHome", "/host/getUserInfo",
                "/host/getUserDynamic", "/host/search", "/host/getHostCallContents",
                "/dynamic/getDynamics", "/base/getBaseInfo",
                "/top/getUserTop", "/im/getGifts");

        //处理用户在线状态
        registry.addInterceptor(userStatusInterceptor).addPathPatterns("/**");
    }


    /**
     * 开启WebSocket支持
     * @Author Shane
     * @Date 2019/8/2616:38
     * @param
     * @return
     **/
    /*@Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }*/
}
